<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar">
      <el-form :inline="true" :model="filters">
        <el-row class="search-input">
         <!-- <el-form-item label="ID：">
            <el-input type="text" oninput="value=value.replace(/\D/g,'')" v-model="filters.id" clearable placeholder="ID搜索"></el-input>
          </el-form-item>-->
          <el-form-item label="名称：">
            <el-input v-model="filters.title" clearable placeholder="名称搜索"></el-input>
          </el-form-item>
          <el-form-item label="状态：">
            <el-select v-model="filters.status">
              <el-option label="草稿" value="0">草稿</el-option>
              <el-option label="已发布" value="1">已发布</el-option>
              <el-option label="下架" value="2">下架</el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="修改时间：" prop="start">
            <el-date-picker
              v-model="filters.updateTimeStart"
              style="width:100%"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期"
            ></el-date-picker>
          </el-form-item>
          <!--<el-form-item label="修改结束时间：" prop="end">
            <el-date-picker
              v-model="filters.updateTimeEnd"
              style="width:100%"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期"
            ></el-date-picker>
          </el-form-item>-->
        </el-row>
        <el-row class="search-button">
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(filters)"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-refresh" :label="$t('action.reset')"
              @click="reset"
              type="primary"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-hand-pointer-o"
              :label="$t('action.add')"
              @click="handleAdd"
              type="primary"
            />
          </el-form-item>
        </el-row>
        <el-form-item class="t_r w100">
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
        <table-column-filter-dialog
          ref="tableColumnFilterDialog"
          :columns="columns"
          @handleFilterColumns="handleFilterColumns"
        ></table-column-filter-dialog>
      </el-form>
    </div>
    <!--表格内容栏-->
    <jic-table
      :height="350"
      :data="pageResult"
      :columns="filterColumns"
      @findPage="findPage"
      ref="JicTable"
      @handleEdit="handleEdit"
      @handleDelete="handleDelete"
      @handleCopy="handleCopy"
      @handleLower="handleLower"
      @grounding="grounding"
      :showSelection="false"
      :showOperation="true"
      :showCopyOperation="true"
      :showPushOperation=false
      :showPushOperations="true"
      :groundings="true"
      :showLowerOperation="true"
      :showBatchDelete="false"
      :operationWidth="400"
      :showCopyLink="true"
    ></jic-table>
  </div>
</template>
<script>
import PopupTreeInput from "@/components/PopupTreeInput";
import JicTable from "@/views/Core/JicTable";
import KtButton from "@/views/Core/KtButton";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
import { format } from "@/utils/datetime";
import axios from "axios";
export default {
  components: {
    PopupTreeInput,
    JicTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      imgUpload: this.utils.getUpLoadHost(),
      size: "small",
      document_license: "",
      filters: {
        name: ""
      },

      imgs: "",
      fileList: {},
      columns: [],
      shoplist: [],
      filterColumns: [],
      selected: "",
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},
      dialogImageUrl: "",

      operation: false, // true:新增, false:编辑
      dialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      printPage: false,
      dataFormRules: {},
      pageType:3,
    };
  },
  methods: {
    // 获取分页数据
    findPage: function(data) {
      /**
       * 子组件传过来的分页参数
       * data.start 当前页
       * data.limit 当前页总条数
       * */
      if (data != null && data.start != null) {
        if (data.start != null) {
          this.pageRequest.pageNum = data.start;
        }
        if (data.limit != null) {
          this.pageRequest.pageSize = data.limit;
        }
      }

      let this_ = this;
      if (this_.filters === undefined || this_.filters === null) {
        this_.filters = {};
      }
      this_.filters.platform = 1;
      this_.filters.start = this.pageRequest.pageNum;
      this_.filters.limit = this.pageRequest.pageSize;
      if(this_.pageType){
        this_.filters.pageType = this_.pageType;
      }
      this.utils.request.getCmsPageList(this_.filters, function(res) {
        this_.$refs.JicTable.findPage({
          content: res.data.rows,
          total: res.data.total
        });
      });
    },

    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "省市县列表";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    },

    reset: function() {
      this.filters = {};
      this.findPage();
    },

    // 时间格式化
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property]);
    },

    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      this.columns = [
        { prop: "id", label: "ID", minWidth: 120 },
        { prop: "title", label: "名称", minWidth: 120 },
        /*{
          prop: "pageType",
          label: "类型",
          minWidth: 120,
          formatter: this.pageType
        },*/
        // {prop: "platform", label: "平台", minWidth: 120, formatter: this.platform},
        {
          prop: "status",
          label: "状态",
          minWidth: 120,
          formatter: this.status
        },
        // {prop: "creator", label: "创建人", minWidth: 120},
        /*{prop:"storeName", label:"店铺名称", minWidth:120},*/
        // { prop: "updateTime", label: "修改时间", minWidth: 120 }
      ];
      const temp = [];
      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    // pageType: function(row, column, cellValue, index) {
    //   if (cellValue === 1) {
    //     return "平台首页";
    //   } else if (cellValue === 2) {
    //     return "文章页";
    //   } else if (cellValue === 3) {
    //     return "营销页";
    //   } else if (cellValue === 4) {
    //     return "店铺首页";
    //   } else {
    //     return "店铺营销页";
    //   }
    // },
    platform: function(row, column, cellValue, index) {
      if (cellValue === 1) {
        return "H5";
      } else if (cellValue === 2) {
        return "PC";
      }
    },
    status: function(row, column, cellValue, index) {
      if (cellValue === 0) {
        return "草稿";
      } else if (cellValue === 1) {
        return "已发布";
      } else if (cellValue === 2) {
        return "下架";
      }
    },
    // 显示新增界面
    handleAdd: function(productId) {
      const this_ = this;
      const params = { platform: 1,pageType:this_.pageType || 1}; //h5平台页面
      this_.utils.request.addCmsPage(params, function(res) {
        if (res.code === "0000") {
          this_.$router.push({
            //              path: '/CmsPageTemplates/'
            path: "/MIndexProductShow",
            query: { pageId: res.data }
          });
        } else {
          this_.$message({ message: "后台异常", type: "error" });
        }
      });
    },
    // 编辑
    handleEdit: function(row) {
      this.$router.push({
        path: "/MIndexProductShow",
        query: { pageId: row.row.id }
      });
    },
    // 复制
    handleCopy: function(row) {
      this.$confirm("确认复制选中记录吗？", "提示", {
        type: "warning"
      })
        .then(() => {
          const params = {};
          params.id = row.row.id;
          const this_ = this;
          this.utils.request.copyCmsPage(params, function(res) {
            if (res.code === "0000") {
              this_.$message({ message: "操作成功", type: "success" });
              this_.findPage(null);
            } else {
              this_.$message({
                message: "操作失败, " + res.msg,
                type: "error"
              });
            }
          });
        })
        .catch(() => {
          console.log("3333333");
        });
    },
    // 发布
   /* handlePush: function(row) {
      this.$confirm(
        "确认发布选中记录吗？一旦发布,将替换已发布的展示页面",
        "提示",
        {
          type: "warning"
        }
      )
        .then(() => {
          const params = {};
          params.id = row.row.id;
          const this_ = this;
          this.utils.request.pushCmsPage(params, function(res) {
            if (res.code === "0000") {
              this_.$message({ message: "操作成功", type: "success" });
              this_.findPage(null);
            } else {
              this_.$message({
                message: "操作失败, " + res.msg,
                type: "error"
              });
            }
          });
        })
        .catch(() => {
          console.log("3333333");
        });
    },*/
    // 下架
    handleLower: function(row) {
      this.$confirm("确认下架选中记录吗？", "提示", {
        type: "warning"
      })
        .then(() => {
          const params = {};
          params.id = row.row.id;
          const this_ = this;
          this.utils.request.lowerCmspage(params, function(res) {
            if (res.code === "0000") {
              this_.$message({ message: "操作成功", type: "success" });
              this_.findPage(null);
            } else {
              this_.$message({
                message: "操作失败, " + res.msg,
                type: "error"
              });
            }
          });
        })
        .catch(() => {
          console.log("3333333");
        });
    },
    // 上架
    grounding: function(row) {
      this.$confirm("确认上架选中记录吗？", "提示", {
        type: "warning"
      })
        .then(() => {
          const params = {};
          params.id = row.row.id;
          params.pageType = 3;
          const this_ = this;
          this.utils.request.lowerSoldOut(params, function(res) {
            if (res.code === "0000") {
              this_.$message({ message: "操作成功", type: "success" });
              this_.findPage(null);
            } else {
              this_.$message({
                message: "操作失败, " + res.msg,
                type: "error"
              });
            }
          });
        })
        .catch(() => {
          console.log("3333333");
        });
    },
    // 删除
    handleDelete: function(row) {
      const params = {};
      params.id = row.params[0].id;
      const this_ = this;
      this.utils.request.deleteCmsPage(params, function(res) {
        if (res.code === "0000") {
          this_.$message({ message: "操作成功", type: "success" });
          this_.findPage(null);
        } else {
          this_.$message({ message: "操作失败, " + res.msg, type: "error" });
        }
      });
    }
  },
  mounted() {
    this.findPage();
    this.initColumns();
    if(this.$route.query.pageType){
      this.pageType = this.$route.query.pageType;
    }
  }
};
</script>
<style lang="scss" scoped>
</style>
